<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>apache 开启压缩功能 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>apache 开启压缩功能</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的压缩并不是对网站的图片压缩，而是对普通的静态文件，诸如html, js, css 等元素压缩。不要小看这个压缩功能，如果一个网站的请求量很大的话，这样可以节省海量带宽，在我国带宽资源非常昂贵，所以小小的一个压缩功能可以为企业节省不少的成本呢！下面就来看看如何配置它？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，需要看一下我们的apache是否支持压缩功能。 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/apache2/bin/apachectl -l</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看看是否有mod_deflate</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/apache2/bin/apachectl -l</span></div><div class="line">Compiled <span class="keyword">in</span> modules:</div><div class="line">core.c</div><div class="line">mod_authn_file.c</div><div class="line">mod_authn_default.c</div><div class="line">mod_authz_host.c</div><div class="line">mod_authz_groupfile.c</div><div class="line">mod_authz_user.c</div><div class="line">mod_authz_default.c</div><div class="line">mod_auth_basic.c</div><div class="line">mod_include.c</div><div class="line">mod_filter.c</div><div class="line">mod_log_config.c</div><div class="line">mod_env.c</div><div class="line">mod_setenvif.c</div><div class="line">mod_version.c</div><div class="line">prefork.c</div><div class="line">http_core.c</div><div class="line">mod_mime.c</div><div class="line">mod_status.c</div><div class="line">mod_autoindex.c</div><div class="line">mod_asis.c</div><div class="line">mod_cgi.c</div><div class="line">mod_negotiation.c</div><div class="line">mod_dir.c</div><div class="line">mod_actions.c</div><div class="line">mod_userdir.c</div><div class="line">mod_alias.c</div><div class="line">mod_so.c</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果这里没有，那继续看一下。下面有没有 mod_deflate.so 这个文件 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># ls /usr/local/apache2/modules/</span></div><div class="line">httpd.exp libphp5.so mod_deflate.so mod_expires.so mod_rewrite.so</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果这里也没有，那说明你的apache不支持压缩，需要重编译一下，或者扩展形式安装，或者重新编译apache, 需要在编译的时候，加上  <code>--enable-deflate=shared</code> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好，如果你的apache有了deflate这个模块支持，也就支持了压缩功能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面该配置httpd.conf 了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/httpd.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在httpd.conf 中增加 ：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">LoadModule deflate_module modules/mod_deflate.so</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后再增加如下配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">&lt;ifmodule mod_deflate.c&gt;</div><div class="line">DeflateCompressionLevel 6 <span class="comment">#6是压缩级别，可以选择1-9</span></div><div class="line">AddOutputFilterByType DEFLATE text/plain</div><div class="line">AddOutputFilterByType DEFLATE text/html</div><div class="line">AddOutputFilterByType DEFLATE text/xml</div><div class="line">AddOutputFilterByType DEFLATE text/css</div><div class="line">AddOutputFilterByType DEFLATE text/JavaScript</div><div class="line">AddOutputFilterByType DEFLATE application/xhtml+xml</div><div class="line">AddOutputFilterByType DEFLATE application/xml</div><div class="line">AddOutputFilterByType DEFLATE application/rss+xml</div><div class="line">AddOutputFilterByType DEFLATE application/atom_xml</div><div class="line">AddOutputFilterByType DEFLATE application/x-javascript</div><div class="line">AddOutputFilterByType DEFLATE application/x-httpd-PHP</div><div class="line">AddOutputFilterByType DEFLATE image/svg+xml</div><div class="line">&lt;ifmodule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>为什么要指定文件类型来压缩？</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;答：压缩也是要消耗cpu资源的，图片/视频等文件，压缩效果也不好，一般压缩文本格式。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外上述代码也可以根据实际情况删减。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/57. apache 开启压缩功能/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
